In [2]:
using StatsBase,Statistics,Plots,Distributions
plotlyjs();
In [3]:
p = 0.5
n = 10
s = zeros(n+1);
In [4]:
function path(n)
    x = zeros(n)
    for i in 1:n
        if rand(Binomial(1,p)) == 1
            x[i] = 1
        else
            x[i] = -1
        end
    end
    return x
end
Out[4]:
path (generic function with 1 method)
In [5]:
x = path(10);
In [6]:
function cumulative_sum(x)
    s = zeros(length(x)+1)
    for i in 1:length(x)
        s[i+1] = sum(x[j] for j in 1:i)
    end
    return s
end
Out[6]:
cumulative_sum (generic function with 1 method)
In [7]:
cumulative_sum(x)
Out[7]:
11-element Vector{Float64}:
  0.0
 -1.0
  0.0
 -1.0
  0.0
  1.0
  2.0
  1.0
  0.0
 -1.0
  0.0
In [8]:
s[2:n+1] = cumsum(x) # this is inbuilt function
Out[8]:
10-element Vector{Float64}:
 -1.0
  0.0
 -1.0
  0.0
  1.0
  2.0
  1.0
  0.0
 -1.0
  0.0
In [9]:
rep = 10000
smat = Matrix{Float32}(undef,rep,n+1); # define an empty matrix
In [10]:
smat[:,:1] = zeros(rep); # put zeros in the first column of the matrix
In [11]:
smat[:,:1] = zeros(rep);
x = zeros(n)
for k in 1:rep
    for i in 1:n
        if rand(Binomial(1,p)) == 1
            x[i] = 1
        else
            x[i] = -1
        end
    smat[k,2:n+1] = cumsum(x)
    end
end
In [12]:
smat
Out[12]:
10000×11 Matrix{Float32}:
 0.0  -1.0  -2.0  -1.0  -2.0  -1.0   0.0   1.0   2.0   3.0   2.0
 0.0   1.0   2.0   1.0   0.0  -1.0  -2.0  -1.0   0.0  -1.0  -2.0
 0.0   1.0   0.0  -1.0   0.0  -1.0  -2.0  -3.0  -2.0  -1.0   0.0
 0.0   1.0   2.0   3.0   4.0   3.0   2.0   3.0   2.0   1.0   2.0
 0.0   1.0   0.0  -1.0   0.0   1.0   0.0   1.0   2.0   3.0   2.0
 0.0  -1.0  -2.0  -3.0  -4.0  -5.0  -6.0  -5.0  -6.0  -5.0  -6.0
 0.0  -1.0   0.0   1.0   2.0   3.0   4.0   3.0   4.0   3.0   2.0
 0.0  -1.0  -2.0  -3.0  -2.0  -1.0  -2.0  -1.0   0.0   1.0   2.0
 0.0   1.0   2.0   3.0   4.0   3.0   4.0   3.0   4.0   3.0   4.0
 0.0  -1.0  -2.0  -3.0  -4.0  -3.0  -2.0  -3.0  -4.0  -3.0  -4.0
 0.0  -1.0  -2.0  -3.0  -2.0  -3.0  -2.0  -3.0  -4.0  -5.0  -4.0
 0.0  -1.0   0.0   1.0   2.0   3.0   4.0   3.0   2.0   1.0   2.0
 0.0  -1.0   0.0   1.0   0.0  -1.0   0.0  -1.0  -2.0  -1.0   0.0
 ⋮                             ⋮                             ⋮
 0.0   1.0   0.0   1.0   2.0   1.0   0.0   1.0   2.0   3.0   2.0
 0.0  -1.0   0.0  -1.0   0.0   1.0   0.0   1.0   2.0   3.0   4.0
 0.0   1.0   0.0  -1.0  -2.0  -1.0  -2.0  -3.0  -4.0  -3.0  -4.0
 0.0   1.0   2.0   1.0   0.0  -1.0  -2.0  -3.0  -2.0  -1.0  -2.0
 0.0  -1.0   0.0  -1.0   0.0   1.0   2.0   1.0   2.0   3.0   2.0
 0.0   1.0   2.0   3.0   4.0   5.0   6.0   7.0   6.0   7.0   8.0
 0.0  -1.0  -2.0  -3.0  -4.0  -3.0  -4.0  -3.0  -4.0  -3.0  -2.0
 0.0   1.0   0.0  -1.0   0.0   1.0   2.0   3.0   2.0   1.0   2.0
 0.0   1.0   0.0  -1.0  -2.0  -3.0  -4.0  -5.0  -4.0  -3.0  -4.0
 0.0  -1.0   0.0   1.0   2.0   3.0   4.0   3.0   4.0   5.0   6.0
 0.0   1.0   2.0   1.0   0.0   1.0   0.0  -1.0   0.0  -1.0  -2.0
 0.0   1.0   2.0   1.0   0.0   1.0   0.0   1.0   2.0   1.0   0.0

In the below code we have plotted the matrix smat where each row a random walk paths and we have do this work 10000 times and every time we have started from the origin. So, if we take number of steps is even then we end our random walk in an odd position as every time started from 0. If we take the number of steps is odd then we end our random walk in an even position. This matrix plot describes this.

In [13]:
plot(smat',label = false)
scatter!(repeat([n+1], 2n+1), -n:n, 
    markershape = :circle, markercolor = "red")
plot!(legend =false)
Out[13]:
In [14]:
count_occur = []
for j in -n:2:n
    push!(count_occur,count(i -> (i == j),smat[:,11]))
end
println(count_occur)
Any[13, 96, 469, 1164, 1997, 2427, 2130, 1142, 455, 93, 14]
In [15]:
probability = count_occur/rep
Out[15]:
11-element Vector{Float64}:
 0.0013
 0.0096
 0.0469
 0.1164
 0.1997
 0.2427
 0.213
 0.1142
 0.0455
 0.0093
 0.0014

Below plot is actually is step function.

In [16]:
plot(range(start = -n,stop =n,step = 2),probability)
scatter!(range(start = -n,stop =n,step = 2),probability)
plot!(legend = false)
Out[16]:
In [ ]:

In [ ]: